package com.example.trb.controller.paper;

import com.example.trb.dto.ObjectDTO;
import com.example.trb.entity.Paper;
import com.example.trb.entity.group.Insert;
import com.example.trb.entity.group.Update;
import com.example.trb.service.paper.PaperService;
import com.example.trb.util.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class PaperController {
    @Autowired
    PaperService paperService;
    @Value("${session_user}")
    String userKey;

    @PutMapping("/paper")
    public CommonResult putPaper(@RequestBody @Validated(Update.class) Paper data) {
        paperService.update(data);
        return CommonResult.success(null);
    }

    @DeleteMapping("/paper")
    public CommonResult deletePaper(@RequestBody Paper data) {
        paperService.removeById(data.getId());
        return CommonResult.success(null);
    }

    @GetMapping("/paper")
    public CommonResult getPaper(@RequestParam("id") Integer id) {
        ObjectDTO dto = paperService.getById(id);
        return CommonResult.success(dto.getData());
    }

    @PostMapping("/paper/status")
    public CommonResult status(@RequestParam("id") Integer id,@RequestParam("status") String status)  {
        paperService.updateStatusById(id,status);
        return CommonResult.success(null);
    }

    @PostMapping("/paper")
    public CommonResult addPaper(@RequestBody @Validated(Insert.class) Paper data) {
        ObjectDTO dto = paperService.save(data);
        return CommonResult.success(dto.getData());
    }
}
